package com.sjy.pickphotos.pickphotos.takephoto;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.MediaStore;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import java.io.File;

/**
 * 拍照
 * Created by sjy on 2017/11/22.
 */

public class TakePhotoUtil {

    public static final int REQUEST_CAMERA = 0x223;
    static File outputFile;

    public static void camera(Activity activity) {
        if(activity==null||activity.isFinishing())
            return;
        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        } else {
            outputFile = new File(activity.getExternalCacheDir(), System.currentTimeMillis() + ".png");
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdir();
            }
            Uri contentUri = FileProvider.getUriForFile(activity,
                    activity.getPackageName() + ".fileProvider1", outputFile);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
///storage/emulated/0/Android/data/com.yunlu.salesman/cache/1566291834864.png
///storage/emulated/0/Android/data/com.yunlu.salesman/cache/1566292191662.png
            //添加权限
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
            activity.startActivityForResult(intent, REQUEST_CAMERA);
        }
    }

    public static File onSuccess() {
        return outputFile;
    }


}
